//
//  FYHDrawerViewController.m
//  FollowYourHeart
//
//  Created by lanou3g on 16/3/11.
//  Copyright © 2016年 李晨玲. All rights reserved.
//

#import "FYHDrawerViewController.h"
#import "UIColor.h"


@interface FYHDrawerViewController ()<UIGestureRecognizerDelegate>

@end

@implementation FYHDrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //如果没有手势
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self.tap setEnabled:NO];
        self.tap.delegate = self;
        [self.view addGestureRecognizer:self.tap];
    }

}

//设置根视图控制器
- (void)setRootViewController:(UIViewController *)viewController{
    //如果传进来的viewController是空直接返回
    if (!viewController) {
        return;
    }
    //设置根视图控制器
    [self.rootVC.view removeFromSuperview];
    self.rootVC = viewController;
    [self addChildViewController:viewController];
    //如果手势交互是开着,证明抽屉是打开的
    if (self.tap.enabled) {
        viewController.view.frame = CGRectMake(270, 0, kScreenWidth, kScreenHeight);
    }
    [self.view addSubview:viewController.view];
    //关闭抽屉
    [self showRootViewController:nil];
    //设置导航栏
    [self setNaButton];
}

//展示根视图控制器(关闭抽屉)
- (void)showRootViewController:(UIViewController *)viewController{
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }];
    //手势关闭
    [self.tap setEnabled:NO];
    //rootView交互打开
    self.rootVC.view.userInteractionEnabled = YES;
}

//打开抽屉
- (void)showLeftViewController:(UIViewController *)leftViewController{
    if (![self.view.subviews containsObject:self.leftVC.view]) {
        //插入抽屉的View
        [self.view insertSubview:self.leftVC.view atIndex:0];
    }
    //动画移动根视图控制器
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(270, 0, kScreenWidth, kScreenHeight);
    }];
    //手势打开
    [self.tap setEnabled:YES];
    //rootView禁止使用
    self.rootVC.view.userInteractionEnabled = NO;
}

//设置左上角按钮
- (void)setNaButton{
    UINavigationController *na = (UINavigationController *)self.rootVC;
    UIViewController *vc = na.viewControllers[0];
    vc.navigationItem.leftBarButtonItem = ({
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"meun"] style:(UIBarButtonItemStyleDone) target:self action:@selector(openMeun)];
        item.tintColor = [UIColor colorWithHex:0x11d4a6];
        item;
    });
    
}

//打开抽屉
- (void)openMeun{
    [self showLeftViewController:nil];
    [self.tap setEnabled:YES];
    self.rootVC.view.userInteractionEnabled = NO;
}

//tapAction
- (void)tapAction{
    //关闭抽屉
    [self showRootViewController:nil];
    [self.tap setEnabled:NO];
    self.rootVC.view.userInteractionEnabled = YES;
}

//delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer == self.tap) {
        return CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:_rootVC.view]);
    }
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
